package com.yds.bluetooth

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.le.BluetoothLeScanner
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.os.Build
import android.text.TextUtils
import android.util.Log

/**
 * 低功耗蓝牙扫描
 */
@Suppress("Unused")
 object ScanBleDeviceManager {
    private const val TAG = "YDS蓝牙"

    private var mBluetoothAdapter: BluetoothAdapter? = null
    private var bluetoothLeScanner: BluetoothLeScanner? = null

    // 蓝牙名称是否可以为空
    private var nameCanEmpty: Boolean = false

    // 连接蓝牙设备结果回调
    private var scanResultCallback: ((BluetoothDevice) -> Unit)? = null

    // 扫描到的蓝牙设备
    private val mListBluetoothDevices = mutableListOf<BluetoothDevice>()

    /**
     * 开始扫描
     */
    fun startBleScanDevice(bluetoothAdapter: BluetoothAdapter?, nameCanEmpty: Boolean = false, scanResultCallback: (BluetoothDevice) -> Unit) {
        Log.i(TAG, "开始扫描-低功耗蓝牙设备")
        this.mBluetoothAdapter = bluetoothAdapter
        this.nameCanEmpty = nameCanEmpty
        this.scanResultCallback = scanResultCallback
        mListBluetoothDevices.clear()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            bluetoothLeScanner = bluetoothAdapter?.bluetoothLeScanner
        }
        bluetoothLeScanner?.startScan(leScanCallback)
    }

    /**
     * 终止扫描
     */
    fun stopBleScanDevice() {
        Log.i(TAG, "终止扫描-低功耗蓝牙设备")
        bluetoothLeScanner?.stopScan(leScanCallback)
    }

    /**
     * 扫描结果回调
     */
    private val leScanCallback = object : ScanCallback() {
        override fun onScanResult(callbackType: Int, result: ScanResult?) {
            result?.let {
                val device = it.device
                if (!nameCanEmpty) {
                    if (!TextUtils.isEmpty(device.name)) {
                        addDiscoverDevice(device)
                    } else {
                        Log.i("发现无名称设备", "发现无名称设备: ${device.name} ${device.address}")
                    }
                } else {
                    addDiscoverDevice(device)
                }
            }
        }
    }

    /**
     * 添加发现的蓝牙设备
     */
    private fun addDiscoverDevice(device: BluetoothDevice) {
        if (mListBluetoothDevices.isEmpty()) {
            Log.i(TAG, "发现低功耗蓝牙设备: ${device.name} ${device.address} ${device.type}")
            scanResultCallback?.invoke(device)
            mListBluetoothDevices.add(device)
        } else {
            var hasSameDevice = false
            mListBluetoothDevices.forEach { item ->
                if (item.address == device.address) {
                    hasSameDevice = true
                }
            }
            if (!hasSameDevice) {
                Log.i(TAG, "发现低功耗蓝牙设备: ${device.name} ${device.address} ${device.type}")
                scanResultCallback?.invoke(device)
                mListBluetoothDevices.add(device)
            }
        }
    }

    /**
     * 查询全部搜索到的蓝牙设备
     */
    fun queryAllScanBluetoothDevices(): List<BluetoothDevice> {
        return mListBluetoothDevices
    }

}